<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Video;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Api\V1\Resources\VideoResource;
use App\Supports\LinkedIn\Client;

class VideosController extends Controller
{
	

	public function tingwuNotify()
	{
		
		$res = [
			"Code"=>200,
			"Data"=>["Test"=>"xbreaking"],
			"Message"=>"success",
			"RequestId"=>request("RequestId") ?? '',

		];
		 return response()->json($res);
		//return cashier('alipay')->callbackAlipay();

	}

	public function index()
	{


		$videos = Video::query()->where('is_examine',true)->orderByDesc("id")->paginate(10);
	
				
		return VideoResource::collection($videos);
	}


	public function show($id)
	{
		$video = Video::find($id);
		return new VideoResource($video);
	}


	public function shareToLinkdin($id)
	{

		$new = Video::find($id);
		if(!$new){

			fail('当前视频不存在');
		}
		$user = request()->user();
		$client = new Client(
		    getenv('LINKDIN_CLIENT_ID'),
		    getenv('LINKDIN_CLIENT_SECRET'),
		);
		$client->setRedirectUrl('https://www.xbreakingtechinsight.com');


		
		$client->setAccessToken($user->linkdin_token);


		
		$register = [
			'registerUploadRequest'=>[
				"recipes"=>[
					"urn:li:digitalmediaRecipe:feedshare-video"
				],
				"owner"=>'urn:li:person:' . $user['nickname'],
				"serviceRelationships"=>[
					[
						 "relationshipType"=>"OWNER",
             			  "identifier"=> "urn:li:userGeneratedContent"
            		]
				]
			]

		];
		$register = $client->post(                 
                'assets?action=registerUpload',                         
                $register
            );

		
		if($register['value']){

			$uploadUrl = $register['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'];
			$params = [
				'file'=>img_path($new->video_url),
			];

			$res = $this->curl($params,$uploadUrl);

			$arr = [                                   
                    'author' => 'urn:li:person:' . $user['nickname'],
                    'lifecycleState' => 'PUBLISHED',
                    'specificContent' => [          
                        'com.linkedin.ugc.ShareContent' => [
                            'shareCommentary' => [
                                'text' => $new->brief_en
                            ],
                            'shareMediaCategory' => 'VIDEO',
                            'media' => [
                                [
                                    'status' => 'READY',
                                    'description' => [
                                        'text' => strip_tags($new->brief_en)
                                    ],
                                    "media"=> $register['value']['asset'],

                                    //img_path($new['images']
                                    'originalUrl' => "https://www.xbreakingtechinsight.com/video/".$new->id,
                                    'title' => [
                                        'text' => $new->title_en
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'visibility' => [
                        'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
                    ]
                ];

              
			$share = $client->post(                 
	                'ugcPosts',                         
	                $arr
	            );

			//var_dump($share);

		}

		
		
		return response()->json(['code' => 200]);
	}
	

	public function curl($data,$url){

		$curl = curl_init();
		$data = json_encode($data);


		curl_setopt_array($curl, array(
		   CURLOPT_URL => $url,
		   CURLOPT_RETURNTRANSFER => true,
		   CURLOPT_ENCODING => '',
		   CURLOPT_MAXREDIRS => 10,
		   CURLOPT_TIMEOUT => 0,
		   CURLOPT_FOLLOWLOCATION => true,
		   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		   CURLOPT_CUSTOMREQUEST => 'POST',
		   CURLOPT_POSTFIELDS =>$data,
		   CURLOPT_HTTPHEADER => array(
		      'Authorization: Bearer redacted'
		   ),
		));

	   $response = curl_exec($curl);

	   //dd($url);
	   return $response;
	}
}
